/**
 * Copyright © 2015 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.ieditor.manager.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.eqxiu.ieditor.api.ITagServiceApi;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.dto.TagDTO;
import com.eqxiu.ieditor.manager.utils.RequestUtils;
import com.eqxiu.ieditor.manager.utils.UserContext;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @description 标签接口
 * @author ranger
 * @date Jun 8, 2017
 */
@Controller
@RequestMapping("/m/ie/tag")
public class TagController {
	@Autowired
	private ITagServiceApi tagServiceApi;

	@PostMapping("/{page}")
	public String index(@PathVariable String page) {
		return "tag/" + page;
	}

	@PostMapping("/list")
	@ResponseBody
	public Map<String, Object> findOrderList(HttpServletRequest request, HttpServletResponse response) {
		// 按条件过滤
		Map<String, String> params = RequestUtils.getRequestParamMap(request);
		return tagServiceApi.findByParams(params);
	}

	@PostMapping(value = "/save")
	@ResponseBody
	public ResultData saveTag(TagDTO params) {
		try {
			params.setCreateUser(UserContext.getUser() != null ? UserContext.getUser().getId() : "");
			List<TagDTO> tagDTOs = Lists.newArrayList();
			if (params.getId() != null) {
				TagDTO dto = new TagDTO();
				dto.setId(params.getId());
				tagDTOs = tagServiceApi.selectByCondition(dto);
			}

			if (tagDTOs != null && tagDTOs.size() > 0) {
				tagServiceApi.update(params);
			} else {
				tagServiceApi.save(params);
			}
		} catch (Exception e) {
			ResultData.result(false);
		}
		return ResultData.result(true);
	}

	@PostMapping("/delete")
	@ResponseBody
	public ResultData deleteBrand(HttpServletRequest request, HttpServletResponse response) {
		tagServiceApi.deleteByIds(request.getParameter("ids"));
		return ResultData.result(true);
	}

}
